// 格式化日期为 'YYYY-MM-DD HH:mm:ss'，自动转为北京时间（东八区）
function getDateTime(date = new Date()) {
  const gmtDate = date.getTime() + date.getTimezoneOffset() * 60000;
  const utcDate = new Date(gmtDate + 8 * 3600000);

  const pad = (n) => (n < 10 ? '0' + n : n);

  return (
    utcDate.getFullYear() +
    '-' +
    pad(utcDate.getMonth() + 1) +
    '-' +
    pad(utcDate.getDate()) +
    ' ' +
    pad(utcDate.getHours()) +
    ':' +
    pad(utcDate.getMinutes()) +
    ':' +
    pad(utcDate.getSeconds())
  );
}

module.exports = {
  getDateTime,
};
